package com.eroad.offer.tencent;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.eroad.offer.sina.LoginInfo;
import com.eroad.offer.sina.OtherLogin;
import com.sky.widget.SHToast;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQAuth;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

/**
 * 腾讯QQ登陆
 * 
 * @author demon
 * 
 */
public class TencentLogin {

	private Context context;

	private static String mAppid;

	private static QQAuth mQQAuth;

	private Tencent mTencent;

	private UserInfo mInfo;

	private String nickname, access_token, openid, headImage;

	private LoginInfo loginInfo;

	public TencentLogin(Context c) {
		this.context = c;

		loginInfo = new LoginInfo();

		mAppid = AppConstants.APP_ID;

		mQQAuth = QQAuth.createInstance(mAppid, context);

		mTencent = Tencent.createInstance(mAppid, context);
		//			mQQAuth.logout(context);

	}

	public void login() {
		if (!mQQAuth.isSessionValid()) {
			IUiListener listener = new BaseUiListener() {
				@Override
				protected void doComplete(JSONObject values) {
					System.out.println("doComplete");
					updateUserInfo();
				}
			};
			mTencent.loginWithOEM((Activity) context, "all", listener,
					"101124918", "101124918", "xxxx");

		} else {
			updateUserInfo();
		}
	}

	private class BaseUiListener implements IUiListener {

		@Override
		public void onComplete(Object response) {
			// 获取用户授权的access_token值
			System.out.println("qqloginonComplete"+response);
			try {
				JSONObject object = new JSONObject(response.toString());
				access_token = object.getString("access_token");// 用户的access_token
				openid = object.getString("openid");// QQ用户的唯一标识

			} catch (JSONException e) {
				e.printStackTrace();
			}
			doComplete((JSONObject) response);
		}

		protected void doComplete(JSONObject values) {

		}

		@Override
		public void onError(UiError e) {
			System.out.println("error:==="+e.errorMessage);
			Util.toastMessage((Activity) context, e.errorMessage);
			Util.dismissDialog();
		}

		@Override
		public void onCancel() {
			System.out.println("取消登录");
			Util.toastMessage((Activity) context, "取消登录");
			Util.dismissDialog();
		}
	}

	private void updateUserInfo() {
		if (mQQAuth != null && mQQAuth.isSessionValid()) {
			IUiListener listener = new IUiListener() {

				@Override
				public void onError(UiError e) {
					System.out.println("qqlogin"+e);
				}

				/**
				 * 获取到用户的信息
				 */
				@Override
				public void onComplete(final Object response) {

					try {
						JSONObject object = new JSONObject(response.toString());
System.out.println("获取到用户的信息"+object.toString());
						nickname = object.getString("nickname");
						
						headImage = object.getString("figureurl_1");
						System.out.println("ssss"+openid+"==="+access_token+"=="+object);
						loginInfo.setNickname(nickname);
						loginInfo.setOpenid(openid);
						loginInfo.setToken(access_token);
						loginInfo.setType("qq.com");
//						loginInfo.setUserphoto(object.);
						loginInfo.setUserphoto(headImage);
						new OtherLogin(loginInfo, context).login();
						SHToast.showToast(context, "登录成功", Toast.LENGTH_SHORT);
						mQQAuth.logout(context);

					} catch (JSONException e) {
						e.printStackTrace();
					}

					System.out.println("qq用户的信息：" + "昵称：" + nickname + "，唯一标识："
							+ openid + "，用户的头像：" + headImage);

				}

				@Override
				public void onCancel() {
					SHToast.showToast(context, "取消成功", Toast.LENGTH_SHORT);
				}
			};

			mInfo = new UserInfo(context, mQQAuth.getQQToken());
			mInfo.getUserInfo(listener);
		} else {
			SHToast.showToast(context, "登录失败", Toast.LENGTH_SHORT);
		}
	}

}
